package com.fileutils;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.Uri;

import java.io.*;

/**
 * Created by Administrator on 2019/4/12.
 */

public class ImageFileUtil {
    /**
     * 相机和相册图片解析
     *
     * @param path
     * @param status true 相机  false  相册
     * @return
     */
    public static Bitmap analyzePic(Context activity, CustomClass path, boolean status) {
        Bitmap selectedBitmap = null;
        if (status) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path.getStringName(), options);
            options.inJustDecodeBounds = false;
            options.inSampleSize = calculateSampleSize(options);
            selectedBitmap = BitmapFactory.decodeFile(path.getStringName(), options);
        } else if (!status) {
            ContentResolver cr =activity.getContentResolver();
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(cr.openInputStream(path.getUriName()), new Rect(), options);
                options.inJustDecodeBounds = false;
                options.inSampleSize = calculateSampleSize(options);
                selectedBitmap = BitmapFactory.decodeStream(cr.openInputStream(path.getUriName()), new Rect(), options);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return selectedBitmap;
    }

    /**
     * 根据路径获得图片信息并按比例压缩，返回bitmap
     */
    private static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;//只解析图片边沿，获取宽高
        BitmapFactory.decodeFile(filePath, options);
        // 计算缩放比
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // 完整解析图片返回bitmap
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    private static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    /**
     * @param filePath
     * @param maxLenDp 长宽中较小的度量， 最大不能超过的dp值
     * @return
     */
    public static Bitmap compressBitmap(String filePath, int maxLenDp) {
        // 设置参数
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // 只获取图片的大小信息，而不是将整张图片载入在内存中，避免内存溢出
        BitmapFactory.decodeFile(filePath, options);
        int height = options.outHeight;
        int width = options.outWidth;
        int inSampleSize = 2; // 默认像素压缩比例，压缩为原图的1/2
        int minLen = Math.min(height, width); // 原图的最小边长
        if (minLen > maxLenDp) { // 如果原始图像的最小边长大于100dp（此处单位我认为是dp，而非px）
            float ratio = (float) minLen / maxLenDp; // 计算像素压缩比例
            inSampleSize = (int) ratio;
        }
        options.inJustDecodeBounds = false; // 计算好压缩比例后，这次可以去加载原图了
        options.inSampleSize = inSampleSize; // 设置为刚才计算的压缩比例
        Bitmap bm = BitmapFactory.decodeFile(filePath, options); // 解码文件
        return bm;
    }

    /**
     * 设置图片缩放比例
     *
     * @param options
     * @return
     */
    private static int calculateSampleSize(BitmapFactory.Options options){
        int outHeight = options.outHeight;
        int outWidth = options.outWidth;
        int sampleSize = 1;
        int destHeight = 1000;
        int destWidth = 1000;
        if (outHeight > destHeight || outWidth > destHeight) {
            if (outHeight > outWidth) {
                sampleSize = outHeight / destHeight;
            } else {
                sampleSize = outWidth / destWidth;
            }
        }
        if (sampleSize < 1) {
            sampleSize = 1;
        }
        return sampleSize;
    }

    /**
     * 保存图片
     * @param bitmap
     * @param saveFile
     */
    public static void saveImage(Bitmap bitmap, File saveFile){
        try {
            FileOutputStream fos = new FileOutputStream(saveFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 主体功能：照片路径实体类
     */
    public static class CustomClass implements Serializable {
        private boolean isTakePhoto;
        private String stringName;
        private Uri uriName;

        public String getStringName() {
            return stringName;
        }

        public CustomClass setStringName(String stringName) {
            this.stringName = stringName;
            return this;
        }

        public Uri getUriName() {
            return uriName;
        }

        public CustomClass setUriName(Uri uriName) {
            this.uriName = uriName;
            return this;
        }

        public boolean isTakePhoto() {
            return isTakePhoto;
        }

        public CustomClass setTakePhoto(boolean takePhoto) {
            isTakePhoto = takePhoto;
            return this;
        }
    }
}
